﻿//引入存取文件的路由
const upload = require('./routes/upload_file')
const screen = require('./routes/screen/screen')
const unipush = require('./routes/push/unipush')
var express = require('express');

var request=require('request')
var base_url="http://192.168.110.159:8080/casd2/admin/"

var app = express();

unipush()
//post引用
var bodyParser = require('body-parser');

var compression = require('compression')


app.engine('html',require('express-art-template'))

app.use(compression())


app.use('/',express.static("./public/"))
app.use(bodyParser.urlencoded({ extended: false }));
//Content-Type, Content-Length, Authorization, Accept, X-Requested-With
app.all('*',function(req,res,next){
  res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , token');
    res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
  if (req.method == 'OPTIONS') {
    res.send(200); 
  }
  else {
    next();
  }
})
app.use(upload)
app.use(screen)

app.get('/qr',function(req,res){
  res.send("qr")
})

app.get('/screen',function(req,res){
  res.render('screen.html')
})

app.get('/userWagesLists',(req,res)=>{
  var myDate = new Date();
  var totalDay = new Date(
    myDate.getFullYear(),
    parseInt(myDate.getMonth()),
    0
  ).getDate();
  let options = {
    url: base_url+'userWagesLists',
    method: 'GET',
    headers: {
      "content-type": "application/json",
      "token":req.headers.token
  },
    qs:req.query
    };
  request(options,(err,res2,body)=>{
    let data=JSON.parse(body)
    data.data.forEach(item=>{
      item.finance_wages_vacaCount=totalDay-item.finance_wages_attCount-item.finance_wages_leaveCount
      if (item.uc_wage_actualDay === 0) {
        item.uc_wages_dedu = (
          ((item.uc_wage_base + item.uc_wage_post) / totalDay) *
          item.finance_wages_leaveCount
        ).toFixed(2);
      } else {
        item.uc_wages_dedu = (
          ((item.uc_wage_base + item.uc_wage_post) / totalDay) *
          (totalDay - item.uc_wage_actualDay)
        ).toFixed(2);
      }
      item.uc_wages_baseTotal = (
        item.uc_wage_post +
        item.uc_wage_base +
        item.uc_wage_subsidy -
        item.uc_wages_dedu
      ).toFixed(2);
      item.uc_wage_realhair =
                (item.uc_wages_baseTotal - item.uc_wage_tax - item.uc_wage_socSec).toFixed(2);
      return item
    })
    res.send(data)
  })
})

app.get('/IQC',function(req,res){
	res.render('IQC.html')
	})

app.get('/', function (req, res) {
    res.render('index.html')
})

var server = app.listen(8089,function () {
  console.log("running")
})

server.setTimeout(0)

